﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class AllReturnToFactoryList : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }
        public int PageCount { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["PageIndex"] = 0;
                BindForm();
            }
        }

        private void BindForm()
        {
            ReturnToFactoryService service = new ReturnToFactoryService();
            RecordsNumber = service.GetCountOfReturnToFactoryForms(keyWordTextBox.Text);
            PageCount = RecordsNumber / 20;
            Session["PageCount"] = RecordsNumber / 20;
           
            returnToFactoryFormsGridView.DataSource = service.GetAllForms(keyWordTextBox.Text.Trim(), Convert.ToInt32(Session["PageIndex"]), 20);
            returnToFactoryFormsGridView.DataBind();
        }

        protected void rtfLinkButton_Click(object sender, EventArgs e)
        {
            string returnToFactoryID = ((LinkButton)sender).CommandArgument;
            Response.Redirect(string.Format("AllReturnToFactoryDetail.aspx?{0}={1}", Constants.QUERYSTR_RETURNTOFACTORY_ID, returnToFactoryID));
        }

        protected void returnToFactoryFormsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //if (e.NewPageIndex >= 0 && e.NewPageIndex < returnToFactoryFormsGridView.PageCount)
            //{
            //    returnToFactoryFormsGridView.PageIndex = e.NewPageIndex;
            //}
           
            //BindForm();
        }

        protected void returnToFactoryFormsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedIndex = Convert.ToInt32(Session["PageIndex"]);
            }
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            //returnToFactoryFormsGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            Session["PageIndex"] = pageDDL.SelectedIndex;
            BindForm();
        }

        protected void resetLinkButton_Click(object sender, EventArgs e)
        {
            keyWordTextBox.Text = string.Empty;
            BindForm();
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            ReturnToFactoryService service = new ReturnToFactoryService();
            returnToFactoryFormsGridView.DataSource = service.GetReturnToFactoryFormsByKeywords(keyWordTextBox.Text);
            returnToFactoryFormsGridView.DataBind();
        }

        protected void returnToFactoryFormsGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            ReturnToFactoryService service = new ReturnToFactoryService();
            IQueryable<ReturnToFactoryForm> forms = service.GetAllForms().AsQueryable();
            string sortDirection = "asc";
            if (Session["OrderDirection"] != null)
            {
                sortDirection = Session["OrderDirection"].ToString();
                if (sortDirection.Equals("asc"))
                {
                    Session["OrderDirection"] = "desc";
                }
                else
                {
                    Session["OrderDirection"] = "asc";
                }
            }
            else
            {
                Session["OrderDirection"] = "asc";
            }
            returnToFactoryFormsGridView.DataSource = forms.OrderBy(string.Format("{0} {1}", e.SortExpression, sortDirection)).ToList();
            returnToFactoryFormsGridView.DataBind();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Session["PageIndex"] = 0;
            BindForm();
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            if (!Session["PageIndex"].Equals("0"))
            {
                Session["PageIndex"] = Convert.ToInt32(Session["PageIndex"]) - 1;
            }

            BindForm();
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            if (!Session["PageIndex"].Equals(Convert.ToInt32(Session["PageCount"]) - 1))
            {
                Session["PageIndex"] = Convert.ToInt32(Session["PageIndex"]) + 1;
            }
            BindForm();
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            Session["PageIndex"] = Convert.ToInt32(Session["PageCount"]) - 1;
            BindForm();
        }
    }
}
